package cs.sy.service.servlet;

import java.io.IOException;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cs.sy.common.util.TimeTools;
import cs.sy.service.bean.UserBean;
import cs.sy.service.business.UserLoginBuss;

/**
 * 作者： sunhouji
 * 时间： 2011-8-4 下午12:42:03
 */
public class OrderFoodServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		if(!filterTime()){
			req.setAttribute("alldata", new UserLoginBuss().getAllDataByDate());
			req.setAttribute("date", TimeTools.getDate());
			req.setAttribute("result", "点餐时间已经过去...");
			req.getRequestDispatcher("/foodDetail.jsp").forward(req, resp);
		}else{
			try{
				String c_id = req.getParameter("foodname");
				UserBean userBean = (UserBean) req.getSession().getAttribute("user");
				String u_id = userBean.getId()+"";
				if(new UserLoginBuss().getDataByDay(u_id) != null){
					req.setAttribute("date", TimeTools.getDate());
					req.setAttribute("result", "每日一餐，系统规定...");
					req.setAttribute("alldata", new UserLoginBuss().getAllDataByDate());
					req.getRequestDispatcher("/foodDetail.jsp").forward(req, resp);
				}else{
					if(new UserLoginBuss().insertOrderDetail(u_id, c_id)){
						req.setAttribute("alldata", new UserLoginBuss().getAllDataByDate());
						req.setAttribute("date", TimeTools.getDate());
						req.getRequestDispatcher("/foodDetail.jsp").forward(req, resp);
					}else{
						req.setAttribute("result", "内部错误...");
						req.getRequestDispatcher("/foodDetail.jsp").forward(req, resp);
					}
				}
			}catch (Exception e) {
				req.setAttribute("result", "内部错误,请重新登录...");
				req.getRequestDispatcher("/foodDetail.jsp").forward(req, resp);
			}
		}
	}
	
	private boolean filterTime(){
		Calendar calendar = Calendar.getInstance();
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
//		if((hour == 12 && minute > 30) || hour > 11){
//			return false;
//		}
		return true;
	}
	
	public static void main(String[] args) {
		Calendar calendar = Calendar.getInstance();
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		System.err.println(hour+":"+minute);
	}
}

